import 'dart:async';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
  Future.delayed(Duration(seconds: 1), () => print('delayed')).then((value) {
    scheduleMicrotask(() => print('micro'));
    print('then');
  }).then((value) {
    print('then 2');
  });

  /*Future(() => print('event 1'));

  scheduleMicrotask(() => print('microtask 1'));

  Future.microtask(() => print('microtask 2'));

  Future.value(123).then((value) => print('microtask 3'));*/

/*  // 等待一秒将打印事件加入Event
  Future.delayed(Duration(seconds: 1), () => print('event 3'));
  // 直接将打印事件加入Event
  Future(() => print('event 1'));
  // 等待0秒将打印事件加入Event
  Future.delayed(Duration.zero, () => print('event 2'));

  scheduleMicrotask(() => print('microtask 1'));
  Future.microtask(() => print('microtask 2'));

  Future.value(123).then((value) => print('microtask 3'));

  print('main 1');
  Future.sync(() => print('sync 1'));
  Future.value(getName());
  print('main2');*/
  runApp(MyApp());
}

String getName() {
  print('get name');
  return 'bob';
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  Future<String> getFuture() {
    return Future(() => 'alice');
    // 等待一秒钟返回值
    // return Future.delayed(Duration(seconds: 1), () => 'alice');
  }

  void _incrementCounter() {
    http.get(Uri(host: 'www.baidu.com')).then((value) => null);
  }

  void _incrementCounter2() {
    http.get(Uri(host: 'www.baidu.cn')).catchError((error) => null);
  }

  void _incrementCounter3() {
    getFuture().then((value) => print(value));
  }

  void _incrementCounter4() {
    getFuture3()
        .then((value) {
          print(value);
          return value * 2;
        })
        .then((value) => print(value))
        .catchError((err) => print(err))
        .whenComplete(() => print('complete'));
  }

  Future<String> getFuture2() {
    return Future.error(Exception('something went wrong'));
  }

  Future<int> getFuture3() {
    return Future.value(100);
  }

  void _incrementCounter5() async {
    int id = await getFuture4();
    print(id);
    id *= 2;
    print(id);
  }

  Future<int> getFuture4() async {
    return 100;
  }

  void _incrementCounter6() {
    Future<int> id = getFuture5();
    print(id);
  }

  Future<int> getFuture5() async {
    return 100;
  }

  void _incrementCounter7() async {
    try {
      int id = await getFuture6();
      print(id);
    } catch (err) {
      print('err:$err');
    }
  }

  Future<int> getFuture6() async {
    throw 'oops';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter7,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
